package collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Created by :Infaraway
 * DATE : 2017/3/26
 * Time : 12:35
 * Funtion :
 */
public class CollectionTest {



    @Test
    public void testTool(){
        Collection collection = new ArrayList();
        Collection collection2 = new ArrayList();
        Person person = new Person("Tom", 22);
        collection.add(person);
        collection.add(new Person("Jerry", 25));
        collection.add(new Person("Mike", 32));
        collection.add("infaraway");

        //1. contains(Object obj)：利用equals方法比较，查看集合中有没有指定元素
        boolean flag = collection.contains(new Person("Mike", 32));
        System.out.println(flag);
        flag = collection.contains(person);
        System.out.println(flag);

        //2. containsAll(Collection coll)利用equals方法比较，查看集合中有没有指定元素的集合
        collection2.add(person);
        collection2.add("infaraway");
        flag = collection.containsAll(collection2);
        System.out.println(flag);

        //3. isEmpty()：检查集合是否为空
        flag = collection.isEmpty();
        System.out.println(flag);

        //4. toArray()转化集合为数组
        Object [] object = collection.toArray();
        System.out.println(object.length);

        //toArray(T[] a)
    }


    /**
     * 1. clear() 清空集合
     * 2. remove() ：移除指定的元素，通过equals() 方法在集合中查找指定的元素
     * 如果元素存在，则移除
     * 3. removeAll(Collection coll) 移除coll中有的元素
     * 4. retainAll(Collection coll) 保存coll中有的元素
     */
    @Test
    public void testRemove(){

        Collection collection = new ArrayList();
        Collection collection2 = new ArrayList();
        Person person = new Person("Tom", 22);
        collection.add(person);
        collection.add(new Person("Jerry", 25));
        collection.add(new Person("Mike", 32));
        collection.add("infaraway");

       /* System.out.println(collection.size());
        collection.remove(person);*/

        System.out.println(collection.size());

        collection2.add(person);
        collection2.add("infaraway");

        collection.removeAll(collection2);
        System.out.println(collection.size());
    }

    /**
     * 在Collection中无法获取指定的元素，但是可以遍历所有的元素
     * 1. 使用增强for循环遍历
     * 2. 使用Iterator迭代器遍历
     *  2.1 获取迭代器对象，调用Collection的iterator方法，获取Iterator接口的实现
     *  2.2 调用Iterator接口方法进行迭代
     */
    @Test
    public void testIterator(){

        Collection collection = new ArrayList();
        Collection collection2 = new ArrayList();
        collection.add("infaraway");

        Person person = new Person("Tom", 22);
        collection.add(person);
        collection.add(new Person("Jerry", 25));
        collection.add(new Person("Mike", 32));

        //使用addAll方法
        collection2.addAll(collection);

       /* System.out.println(collection.size());
        for (Object  o : collection) {
            System.out.println(o);
        }*/

        Iterator it = collection2.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }


    }

    /**
     * add()方法
     */
    @Test
    public void testCollection(){
        Collection collection = new ArrayList();
        collection.add("infaraway");
        collection.add(new Person("Tom", 22));
        collection.add(new Person("Jerry", 25));
        collection.add(new Person("Mike", 32));
    }

}
